package com.jzo2o.orders.manager.xxjob;

import com.jzo2o.api.orders.dto.request.OrderCancelReqDTO;
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.response.OrdersServeResDTO;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * \* User: chy
 * \* Date: 2025/10/7  11:21
 * \* Description:
 * \
 */
@Component
@Slf4j
public class CancelOrderJob {
    @Resource
    private IOrdersManagerService ordersManagerService;
    /**
     * 定时任务：取消订单
     */
    @XxlJob("cancelOrderJob")
    public void cancelOrderJob(){
        try {
            log.info("取消订单任务开始执行");
            //查询所有当前时间的15分钟前的订单
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime cancelTime = now.minusMinutes(15);
            List<Orders> orderList = ordersManagerService.queryServeNoPayList(cancelTime);
            for (Orders orders : orderList) {
                OrderCancelReqDTO reqDTO = new OrderCancelReqDTO();
                reqDTO.setId(orders.getId());
                reqDTO.setCancelReason("订单15分钟未支付取消");
                ordersManagerService.cancelOrder(reqDTO);
            }
            log.info("取消订单任务执行完毕");
        } catch (Exception e) {
            log.error("取消订单任务执行异常", e);
        }
    }
}
